<?php
/**
 * Pipahr
 *
 * @author Chin Win
 * @copyright 2013 pipahr.com
 */
OCP\JSON::checkAppEnabled('calendar');
OCP\JSON::callCheck();
if(!OC_User::isLoggedIn()) {
   echo '您需要先登录';
   exit;
}


$offereventid = JRequest::getInt('eventid', 0);

$tpl_model = new PCalendarModelTpl();
$offertpl = $tpl_model->getAllTpl('offer');

$model = new PCalendarModellistview();
$attachment = $model->getAttachmentList();
$copywith = $model->getCopywith();

$calendar_model = new PCalendarModelCalendar();
$calendar_options = $calendar_model->allCalendars(OC_USER::getUser());

if(!empty($offereventid)){
   $object_model = new PCalendarModelObject();

   $ex_data = $object_model->getTodoExtraData($offereventid);
   if ( !$ex_data ) {
      exit('No events found.');
   }

   $startdate = JHTML::_('date', $ex_data->entry_deadline, 'Y-m-d');
   $starttime = JHTML::_('date', $ex_data->entry_deadline, 'H:i');
   $jobseekid = $ex_data->jobseekerid;
   $jobid = $ex_data->jobid;
   
   $entryenddate = isNullDate($ex_data->entry_enddate) ? '': JHTML::_('date', $ex_data->entry_enddate, 'Y-m-d');

   $jobseekername = $ex_data->jobseekername;
   $jobtitle = $ex_data->job_title;
   $objectid = '';
   
}else{
   $objectid = JRequest::getInt('objectid', 0);
   $jobseekid = JRequest::getInt('jobseekid', 0);
   $jobid = JRequest::getInt('jobid',0);
   list($round, $total) = $model->getInterviewRound($jobseekid);

   if (empty($round) && empty($objectid)) {
      exit('No events found.');
   }
   $jobseekername = $round[$total-1]['jobseekername'];
   $jobtitle = $round[$total-1]['job_title'];
}
$userQuota = PCompanyHelper::getUserQuota();
$sms_left = $userQuota['sms_left'];
$validemail = JRequest::getInt("validemail", 0);
if((!empty($round) && !empty($objectid) || !empty($ex_data))){
   echo'<div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3>'.(!empty($ex_data)?"编辑offer":"发offer").'</h3>
        </div>
        <div class="modal-body medium" id="notify_form" data-tpltype="offer">
        <div class="form-horizontal">
        <div class="control-group" style="margin-bottom:4px;">
           <label class="control-label" for="">姓<i class="jiange"></i>名：</label>
           <div class="controls pull-left" style="margin-left:4px; margin-right:30px;">
              <span><a href="'.OCP\Util::linkToRoute('resume_view', array('id' => $jobseekid,'name'=>JFilterOutput::stringURLUnicodeSlug($jobseekername))).'" target="_blank">'.$jobseekername.'</a></span>
           </div>
           <label class="control-label" for="">职<i class="jiange"></i>位：</label>
           <div class="controls pull-left" style="margin-left:4px;">
              <span><a href="'.OC_Helper::linkToRoute("jobpos_setup", array('id'=>$jobid, 'job_title'=>$jobtitle)).'" target="_blank">'.$jobtitle.'</a></span>
           </div>
        </div>
        <div class="control-group">
           <label class="control-label" for=""><em class="redstar">* </em>入职时间：</label>
           <div class="controls">
              <input type="text" class="input-medium startdate" value="'.(empty($startdate)?'':$startdate).'" placeholder="日期" name="from" id="sendfrom">
              <input type="text" class="input-small" value="'.(empty($starttime)?'':$starttime).'" placeholder="时间" name="fromtime" id="fromtime">
           </div>
           <input type="hidden" value="'.$jobseekid.'" placeholder="" name="jobseekerid">
        </div>
        <div class="control-group">
           <label class="control-label" for="">部<i class="jiange"></i>门：</label>
           <div class="controls">
              <input type="text" value="'.(empty($ex_data->department)?'':$ex_data->department).'" placeholder="" name="department">
           </div>
        </div>'; 

   echo'<div class="control-group">
           <label class="control-label" for="">试用期至：</label>
           <div class="controls">
              <input type="text" class="input-medium" value="'.(empty($entryenddate)?'':$entryenddate).'" placeholder="日期" name="entryend" id="entryend">
           </div>
        </div>
        <div class="control-group">
           <label class="control-label" for="">试用期月薪：</label>
           <div class="controls">
              <input type="text" value="'.(empty($ex_data->entrysalary)?'':$ex_data->entrysalary).'" placeholder="" name="entrysalary" id="entrysalary">  元
           </div>
        </div>
        <div class="control-group">
           <label class="control-label" for="">转正后月薪：</label>
           <div class="controls">
              <input type="text" value="'.(empty($ex_data->salary)?'':$ex_data->salary).'" placeholder="" name="salary" id="salary">  元
           </div>
        </div>
        <div class="control-group">
           <label class="control-label" for="">附<i class="jiange"></i>件：</label>
           <div class="controls interv_uplbox" id="attachment" style="margin-right:0px;">
              <input type="text" value="'.(empty($ex_data->file_name)?'':$ex_data->file_name).'" attachmentid = "'.(empty($ex_data->attachment)?'':$ex_data->attachment).'" placeholder="支持最大4M的pdf、docx、doc、jpeg文件" name="" id="filename" readonly="readonly" style="width:258px;">
              <a href="javascript:;" class="ppp_whitebtn whiteupfile" style="margin-right:7px;">选择文件</a>';
              $dropclass = count($attachment)>0 ? 'dropdown' : '';
           echo '
                 <div class="inviteselectmenubox special '.$dropclass.'">
                    <a data-target="#" data-toggle="'.$dropclass.'" class="dropdown-toggle" href="javascript:;" id="choose_tpl"><b>历史附件</b> <i class="blue_triangle"></i></a>
                    <div id="attachment_list" class="dropdown-menu">';
                    foreach ($attachment as $attachmentlist){
                       echo '<a class="apply" id="'.$attachmentlist['id'].'" href="javascript:;"><span style="display:inline;">'.$attachmentlist['file_name'].'</span><i style="cursor:pointer;" class="delete"></i></a>';
                    }
              echo '</div>
                 </div>
                 <div id="uploadofferForm" class="upd_valdcontent" style="position:relative;">
                    <form id="uploadform" action="'.OC_Helper::linkToRoute('calendar_uploadoffertplfile').'" method="POST" enctype="multipart/form-data" style="position:absolute; top:-34px; width:339px; left:0px;">
                    <input type="file" name="file" id="file" style="width:339px; border:300px solid #000000;" />
                    <input type="hidden" name="objid" id="objid" value="'.(empty($offereventid)?$objectid:$offereventid).'"/>
                    <input type="hidden" name="requesttoken" value="'.PSession::getFormToken().'" id="requesttoken">
                    </form>
                    <table id="uploadTable" class="files" width="360" cellpadding="0" cellspacing="0" border="0" style="margin:auto;margin-left:0px;margin-top:5px;">
                    <tr class="file_upload_template" style="display:none;">
                    <td class="file_name"></td>
                    <td class="file_upload_progress"><div></div></td>
                    </tr>
                    </table>
                 </div>
                 <div id="uploadoffererror" style="padding-top:5px; color:#ff0000;"></div>
            </div>                
       </div>';
              
   echo'<div class="control-group" style="margin-bottom:0px;">
        <label class="control-label" for="">补<i class="jiange"></i>充：</label>
        <div class="controls">
        <textarea style="height:100px;" placeholder="如岗位职责、公司福利、薪酬制度" id="notify_text" name="notify_text">'.(!empty($ex_data->offer_text) ? $ex_data->offer_text : (!empty($offertpl [count($offertpl)-1]['tpl_text']) ? $offertpl[count($offertpl)-1]['tpl_text'] : '')).'</textarea>
        </div>
        </div>
        <div class="control-group">
        <div class="controls">
        <div class="mod_action">
        <a href="javascript:;" id="addtpl">存为模板</a>
        <em class="lineem">|</em>';
        $dropdown = count($offertpl)>0 ? 'dropdown' : '';
        echo '<div class="inviteselectmenubox special '.$dropdown.'">
              <a id="choose_tpl" href="javascript:;" class="dropdown-toggle" data-toggle="'.$dropdown.'" data-target="#"><b>选择模板</b> <i class="blue_triangle"></i></a>
              <div class="dropdown-menu" id="tpl_list">';
        foreach ($offertpl as $offertpllist){
           echo '<a id="'.$offertpllist['id'].'" href="javascript:;"><span class="apply" style="display:inline;">'.$offertpllist['tpl_name'].'</span><i class="delete"></i></a>';
        }
        echo '</div></div><span id="delerror" class="error" style="display:none;">保存失败</span>';

        echo'</div>
             <div class="purposlistbox clearfix" id="purposlistbox" style="width:400px;display:none;">
             <i style="left:18px;"></i>
             <input type="text" name="tplname" placeholder="模板名称" class="pull-left"/>
             <button class="btn pull-left" id="savetpl">保存</button>
             <span class="medium" style="padding:2px 0 0 10px;">最多可保存3个模板</span>  
             <div id="tpl_error" class="error" style="display:none; margin-top:5px;">模板数已达上限，请先删除已存模板</div>
             </div>
             </div>
             </div>';

        echo'<div class="control-group" id="carboncopy">
               <label class="control-label" for="">抄<i class="jiange"></i>送：</label>
               <div class="controls">
                   <input type="text" value="'.(empty($ex_data->copywith)?'':$ex_data->copywith).'" placeholder="请输入要抄送的Email地址，多个地址用英文逗号分隔" name="copywith" style="width:400px;" id="copywith">
        		   <div class="copytoeslect">';
        echo'</div></div></div>';
        
        if(!empty($copywith)){
        echo '<div class="control-group" id="sendmarker">
                <label class="control-label" for="">最近抄送人：</label>
                <div class="controls">';
                    
                       foreach($copywith as $val){
                          echo ' <div class="recentselected" style="cursor:pointer" text="'.$val['copywith'].'">'.$val['copywith'].'</div>';
                       }
        echo   '</div>
             </div>';
        }
        echo '<div class="control-group" id="sendmarker">
                <label class="control-label" for=""></label>
                <div class="controls">
                   <div class="checktoeslect">
                      <label class="checkbox inline">
                         <input type="checkbox" id="marker" value="1"> <b>标记为已发送</b><em>（仅保存信息并改变offer状态，不发送给应聘者）</em>
                      </label>
                   </div>
                </div>
             </div>';

        echo '<div class="sendsety_cbg" style="margin-bottom:4px;">
                 <div class="control-group">
                    <label for="" class="control-label"><em class="redstar">* </em>发送设置：</label>
                    <div style="margin-left:84px;" class="controls">';
                     if(empty($validemail)) {
                        echo '<label class="checkbox medium gray inline" id="sendemaillabel"'.($sms_left > 0 ? 'title="简历缺少Email信息"' : '').'>
                           <input type="checkbox" id="sendemail" name="sendemail" value="email" disabled="disabled">Email
                        </label>';
                     } else {
                        echo '<label class="checkbox medium inline">
                           <input type="checkbox" id="sendemail" name="sendemail" value="email" disabled="disabled" checked>Email
                        </label>';
                     }
                     if($sms_left > 0 ) {
                        echo '<label class="checkbox medium inline">';
                           if(!empty($userQuota['email_sms_settings']['sms_offer_0d'])) {
                              echo '<input type="checkbox" id="sendsms" name="sendsms" value="sms" checked>手机短信(<em>剩余' . $sms_left . '条</em>)';
                           } else {
                              echo '<input type="checkbox" id="sendsms" name="sendsms" value="">手机短信(<em>剩余' . $sms_left . '条</em>)';
                           }
                        echo '</label>';
                     } else {
                        echo '<label class="checkbox medium gray inline">
                           <input type="checkbox" id="sendsms" name="send" value="sms" disabled="disabled">手机短信(剩余0条) <a href="' . OC_Helper::linkToRoute('company_quota') . '" target="_blank" class="buymessage">立即购买</a>
                        </label>';
                     }
                    echo '
                    </div>
                 </div>
              </div>';

        echo'<div class="control-group">
                <div class="controls" style="text-align:right; padding-right:30px;">
           	    <span id="saveerror" class="error" style="display:none;">保存失败</span>';
              if(empty($validemail) && ($sms_left <= 0 || empty($userQuota['email_sms_settings']['sms_offer_0d']))) {
                 echo '<button type="submit" style="cursor:default;" class="ppp_graybtn btn-sure" id="previewoffertpl" eventtype="'.(empty($ex_data)?'addoffer':'editoffer').'">预览</button>';
              } else {
                 echo '<button type="submit" class="ppp_bluebtn btn-sure" id="previewoffertpl" eventtype="'.(empty($ex_data)?'addoffer':'editoffer').'">预览</button>';
              }
        echo '	<a id="cancel" href="javascript:;">取消</a>
                </div>
             </div>
          	 <input type="hidden" name="jobid" value="'.$jobid.'" id="jobid">
          	 <input type="hidden" name="icomp_name" value="'.PFactory::getMasterUser()->name.'" id="icomp_name">
          	 <input name="calendar" value="'.$calendar_options[0]['id'].'" type="hidden" id="calendar">
          	 <input type="hidden" name="jobtitle" value="'.$jobtitle.'" id="jobtitle">
          	 <input id="jobseekerid" type="hidden" value="'.$jobseekid.'" name="jobseekerid">
        	   <input type="hidden" name="jobseekername" value="'.$jobseekername.'" id="jobseekername">
             <input type="hidden" name="objectid" value="'.$objectid.'" id="objectid">
             <input type="hidden" name="eventid" value="'.(empty($offereventid)?'':$offereventid).'" id="eventid">
             </div> ';
   exit();
}
